package com.laikan.reader.bookfriend.activity;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;
import android.widget.TextView;
import com.laikan.library.components.BasicApplication;
import com.laikan.library.utils.AndroidUtils;
import com.laikan.library.utils.DeviceUtil;
import com.laikan.reader.R;
import com.laikan.reader.core.LaikanApplication;

public class SignInPopupWindow extends PopupWindow {

    private View mMenuView;
    private TextView tv_tatol_readroll, tv_add_readroll;
    public SignInPopupWindow(Context context) {
        super(context);
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.signingift, null);
        //取消按钮
        //设置按钮监听
        tv_tatol_readroll = (TextView)mMenuView.findViewById(R.id.tv_tatol_readroll);
        tv_add_readroll = (TextView)mMenuView.findViewById(R.id.tv_add_readroll);

        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LayoutParams.FILL_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.FILL_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xa0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                int height = AndroidUtils.dip2px(BasicApplication.getInstance(), 200.0f);
                int bottom = mMenuView.getHeight() - height;
                int y = (int)event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }else if (y > bottom) {
                        dismiss();
                    }
                }
                return true;
            }
        });

    }

    public void setContentView(String readRoll, String addRoll) {
                tv_tatol_readroll.setText(readRoll);
                tv_add_readroll.setText(DeviceUtil.getHtmlFormatedString(LaikanApplication.getInstance(), R.string.signin_add_readroll, addRoll));
    }
}
